<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'header.php'?>
<style type="text/css">
	#import {
		position: relative;
		top: 1.4px;
	}
</style>
<div class="page-header">
	<h1>
		门锁管理
		<a href="#modal-lock" class="btn btn-info a-custom add-lock" data-toggle="modal">
			<i class="ace-icon fa fa-plus"></i> 新增门锁
		</a>
	</h1>
</div>

<div class="col-xs-12">
	<table id="dynamic-table" class="table table-striped table-bordered table-hover" style="margin-top:10px">
		<thead>
			<tr>
				<th>门锁ID</th>
				<th>门锁类型</th>
				<th>关联班级</th>
				<th>门锁设备ID</th>
				<th>设备秘钥</th>
				<th>设备接口IP</th>
				<th>设备类型ID</th>
				<th>创建时间</th>
				<th>操作</th>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
</div>

<!-- 新增账号弹窗 -->
<div id="modal-lock" class="modal fade">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header no-padding">
				<div class="table-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
						<span class="white">&times;</span>
					</button> <span id="school-name"></span>
					门锁管理
				</div>
			</div>
			<div class="modal-body no-padding">
				<div class="row">
					<div class="col-xs-12">
						<form class="form-horizontal mt10" id="lock-form">
							<input type="hidden" name="id" />
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">门锁类型：</label>
								<div class="col-sm-6">
									<select class="chosen-select form-control tag-input-style" data-placeholder="选择门锁类型" name="type">
										<option value="1">信锐门锁</option>
										<option value="2">电磁门锁</option>
									</select>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">关联班级：</label>
								<div class="col-sm-6">
									<select class="chosen-select form-control tag-input-style" data-placeholder="选择班牌关联的班级" name="class_id">
										<?php foreach($classList as $class): ?>
											<option value="<?=$class['id']?>"><?=$class['class_name']?></option>
										<?php endforeach; ?>
									</select>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">设备秘钥：</label>
								<div class="col-sm-6">
									<input class="form-control" type="text" name="token" placeholder="请填写设备秘钥" />
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">设备接口IP：</label>
								<div class="col-sm-6">
									<input class="form-control" type="text" name="ip" placeholder="请填写设备接口IP" />
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">门锁设备ID：</label>
								<div class="col-sm-6">
									<input class="form-control" type="text" name="device_id" placeholder="请填写门锁设备ID" />
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">设备类型ID：</label>
								<div class="col-sm-6">
									<input class="form-control" type="text" name="device_type" placeholder="请填写设备类型ID" />
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
			<div class="modal-footer no-margin-top" >
				<a href="javascript:;" class="btn btn-sm btn-primary" id="save-lock">
					<i class="ace-icon fa fa-check"></i> 保存
				</a>
			</div>
		</div>
	</div>
</div>

<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'footer.php'?>
<script type="text/javascript">
	$(function(){

		// 保存门锁按钮
		var lockForm = $("#lock-form");
		$("#save-lock").click(function(){
			lockForm.submit();
		})
		var submitUrl = "<?=admin_url('school/save_smart_lock_action')?>";
		form_submit(lockForm, submitUrl, function(res) {
			tips_alert(res.msg, res.success, function(){
				if(res.success) window.location.reload();
			});
		})

		// 添加门锁，清除原数据
		$("body").on("click", ".add-lock", function(){
			lockForm.find("[name=id]").val(undefined)
			lockForm.find("[name=device_id]").val('');
			lockForm.find("[name=desc]").val('');
			// lockForm.find("[name=device_id]").attr('readonly', false);
			lockForm.find("[name=token]").val('');
			lockForm.find("[name=ip]").val('');
			lockForm.find("[name=device_type]").val('');
		})
		// 编辑门锁
		$("body").on("click", ".edit-lock", function(){
			lockForm.find("[name=device_id]").val($(this).data("deviceid"));
			lockForm.find("[name=class_id]").val($(this).data("classid"));
			lockForm.find("[name=type]").val($(this).data("type"));
			lockForm.find("[name=id]").val($(this).data("id"));
			// $("#brand-id").text($(this).data("id"));
			// // 编辑不可修改门锁ID
			// if($(this).data("id") != undefined) {
			// 	lockForm.find("[name=device_id]").attr('readonly', true);
			// }
			lockForm.find("[name=token]").val($(this).data("token"));
			lockForm.find("[name=ip]").val($(this).data("ip"));
			lockForm.find("[name=device_type]").val($(this).data("devicetype"));
		})

		// 门锁表格
		$('#dynamic-table').dt({
			"ajaxUrl": '<?=admin_url('school/get_smart_lock_list')?>',
			"columns": [
				{"data": "id"},
				{"data": "typeName"},
				{"data": "class_name"},
	            {"data": "device_id"},
				{"data": "token"},
				{"data": "ip"},
				{"data": "device_type"},
				{"data": "createtime"},
				{"data": function(ret){
					var action = '';
						action += '<a class="edit-lock green mr20" href="#modal-lock" data-toggle="modal" data-id="' + ret.id + '" data-deviceid="' + ret.device_id + 
							'" data-classid="' + ret.class_id + '" data-type="' + ret.type + '" data-token="' + ret.token + '" data-ip="' + ret.ip + '" data-devicetype="' + ret.device_type + '"><i class="fa fa-edit"></i> 修改</a>';
						action += '<a class="text-danger delete-lock" href="javascript:" data-id=' + ret.id +  ' data-deviceid=' + ret.device_id + '><i class="fa fa-times"></i> 删除</a>';
					return action;
				}}
			]
		});
		// 删除门锁
		$("body").on('click', '.delete-lock', function() {
			var id = $(this).data('id');
			var title = $(this).data('deviceid');
			var posturl = "<?= admin_url("school/delete_smart_lock_action") ?>";
			var data = {
				'id': id
			};
			layer.confirm('确定删除：' + title + '？', { title:"提示", offset: '160px' }, function(){
				ajax_post(posturl, data, function() {
					// 刷新页面
					window.location.reload();
				});
			});
		});
	})
</script>